package com.ie.service.before;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.mapper.before.CartMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartEntity> implements CartService {
    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        Long goodsnum = lambdaQuery()
                .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId())
                .count();
        boolean flag = false;
        if(goodsnum > 0) {
            LambdaUpdateWrapper<CartEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(CartEntity::getShoppingnum, cartEntity.getShoppingnum() + 1)
                    .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                    .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId());
            flag = update(lambdaUpdateWrapper);
        }
        else{
            flag = save(cartEntity);
        }
        if(flag){
            return ResponseResult.getMessageResult(null, "A001");
        }
        else{
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> updateCart(CartEntity cartEntity) {
        //购物车的id列表
        List<Integer> bcid = cartEntity.getBcid();
        //购物车的数量列表
        List<Integer> bshoppingnum = cartEntity.getBshoppingnum();
        ArrayList<CartEntity> cartEntities = new ArrayList<CartEntity>();
        for (int i = 0; i < bcid.size(); i++) {
            CartEntity cartEntity1 = new CartEntity();
            cartEntity1.setId(bcid.get(i));
            cartEntity1.setShoppingnum(bshoppingnum.get(i));
            cartEntities.add(cartEntity1);
        }

        boolean flag = updateBatchById(cartEntities);
        if (flag) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        else{
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> deleteCart(CartEntity cartEntity) {
        boolean flag = removeById(cartEntity.getId());
        if(flag){
            return ResponseResult.getMessageResult(null, "A001");
        }
        else{
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
        boolean flag = removeById(cartEntity.getId());
        if(flag){
            return ResponseResult.getMessageResult(null, "A001");
        }
        return ResponseResult.getMessageResult(null, "A002");
    }
}
